package com.example.appointment.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.appointment.R;
import com.example.appointment.adapter.ChooseCityAdapter;
import com.example.appointment.meta.CitiesBean;
import com.example.appointment.meta.DistrictsBean;
import com.example.appointment.util.Constant;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * 选择城市的界面
 */
public class ChooseCityActivity extends BaseActivity {
    @Bind(R.id.activity_chooser_arealist)
    RecyclerView chooserarealist;
    private ArrayList<DistrictsBean> districtsBean;//城市的对象
    private ChooseCityAdapter adapter;
    private CitiesBean itemvo1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ONITEM_CLICK:
                    //列表的点击事件
                    itemvo1 = (CitiesBean) msg.obj;
                    districtsBean = (ArrayList<DistrictsBean>) DataSupport.where("CityID is ?", itemvo1.getCityID() + "").find(DistrictsBean.class);
                    if (districtsBean.size() > 0) {
                        startActivityForResult(new Intent(ChooseCityActivity.this, ChooseDisActivity.class).putExtra("dislist", districtsBean), 0x111);
                    } else {
                        final DistrictsBean itemvo = new DistrictsBean();
                        itemvo.setDisName("");
//                    citiesBean = (ArrayList<CitiesBean>) DataSupport.where("ProID is ?", itemvo.getProID() + "").find(CitiesBean.class);
                        Intent intent = new Intent();
                        intent.putExtra("city", itemvo1);
                        intent.putExtra("dis", itemvo);
                        setResult(RESULT_OK, intent);
                        ChooseCityActivity.this.finish();
                    }
                    break;

            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 0x111:
                    //列表的点击事件
                    final DistrictsBean itemvo = (DistrictsBean) data.getSerializableExtra("districtsbean");
//                    citiesBean = (ArrayList<CitiesBean>) DataSupport.where("ProID is ?", itemvo.getProID() + "").find(CitiesBean.class);
                    Intent intent = new Intent();
                    intent.putExtra("city", itemvo1);
                    intent.putExtra("dis", itemvo);
                    setResult(RESULT_OK, intent);
                    ChooseCityActivity.this.finish();
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setnotificationColore(R.color.maincolor);//設置导航栏顔色
        setContentView(R.layout.activity_choosearea);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        chooserarealist.setLayoutManager(layoutManager);

        adapter = new ChooseCityAdapter(ChooseCityActivity.this, mHandler);
//        ArrayList<ProvincesBean> provincesbean = (ArrayList<ProvincesBean>) DataSupport.findAll(ProvincesBean.class);
        ArrayList<CitiesBean> citiesBean = (ArrayList<CitiesBean>) getIntent().getSerializableExtra("citylist");

        adapter.setArrayList(citiesBean);
//        loadToast = new LoadToast(SaveCarMaActivity.this);
        chooserarealist.setAdapter(adapter);

//        * 查找所有地区
//        cityVO = new CityVO();
//        List<CitiesBean> citiesbean = DataSupport.findAll(CitiesBean.class);
//        List<DistrictsBean> districtsbean = DataSupport.findAll(DistrictsBean.class);

////        Log.d("iiiiiiiis", "onCreate: " + allcity.size() + allcity.toString());
//        cityVO.setCities(citiesbean);
//        cityVO.setDistricts(districtsbean);
//        cityVO.setProvinces(provincesbean);

        initView();

    }

    /**
     *
     */
    private void initView() {
        setTitile("选择城市");
//        testtext.setText(cityVO.toString());
    }


}
